package com.haizol.rest.template;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.haizol.common.form.MessageSendForm;

@Controller
@RequestMapping
public class IndexRest {

	@Autowired
	SimpMessagingTemplate messagingTemplate;

	@GetMapping
	public String index() {
		return "index";
	}

	@MessageMapping("message")
	public void message(MessageSendForm form) {
		if ("0".equals(form.getToUserId())) {
			// 模拟群发
			for (int i = 1; i < 4; i++) {
				messagingTemplate.convertAndSendToUser(i + "", "/message/list", form.getContent());
			}
		} else {
			messagingTemplate.convertAndSendToUser(form.getToUserId(), "/message/list", form.getContent());
		}

		// TODO 消息异步入库

	}

	@GetMapping("message/chatroom")
	public String messageOnUser(Integer id, ModelMap map) {
		map.put("id", id);// test
		return "chatroom/index";
	}

	@GetMapping("user")
	public String user(Integer id, ModelMap map) {
		map.put("id", id);// test
		return "user";
	}

}
